package pers.wk.producer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.wk.redis.RedisService;

/**
 * 通过实现 ConfirmCallback 接口，消息发送到 Broker 后触发回调，
 * 确认消息是否到达 Broker 服务器，也就是只确认是否正确到达 Exchange 中
 */
@Component
@Slf4j
public class ConfirmCallback implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RedisService redisService;

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        String msgId = correlationData.getId();
        String msg = redisService.getCacheObject(msgId);
        if (ack) {
            log.info(String.format("消息 【msgId：{%s}】【msg：{%s}】 成功发送给Broker", msgId, msg));
        } else {
            log.info(String.format("消息 【msgId：{%s}】【msg：{%s}】发送Broker失败", msgId,msg));
        }
    }
}
